home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MACSHELL
/
MS1
/
COMMANDS
/
FILECMDS.C
< prev
next >
Wrap
Text File
|
1992-12-02
|
15KB
|
571 lines
/*
* MacShell Source File
*
* Copyright (c) 1989, 1990, 1991, 1992 Suick Bay Technologies. All rights reserved.
*
*
* RESTRICTIONS ON MacShell program and source code.
*
* Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
* restricted use by the owner of the CDROM "Disk to the future II".
*
* Ñ╩No permission is granted for any commercial use without the written
* consent of the Suick Bay Technologies.
*
* Ñ╩No permission is granted for any redistribution of any kind use without
* the written consent of the Suick Bay Technologies.
*
* Ñ╩Permission is granted to use this for any personal noncommercial use.
*
* Ñ╩You may not distribute source or executable code at all, nor may you
* distribute it with or within a commercial product without the written
* consent of the Suick Bay Technologies. Please send modifications to
* the author for inclusion in updates to the program. Thanks.
*
*
* MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
* WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
*
* SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
* OR ANY PART THEREOF.
*
* In no event will Suick Bay Technologies be liable for any lost revenue
* or profits or other special, indirect and consequential damages, even if
* Suick Bay Technologies has been advised of the possibility of such damages.
*
* Suick Bay Technologies can be reached at:
*
* 8768 Cottonwood lane
* Maple Grove, MN 55369
* Voice: (612) 425-7025
* AppleLink: D5233
*
*
* No parts of this software may be reproduced or stored in a
* retrieval system or transmitted in any form, or any means,
* electronic, mechanical, photocopying, recording or otherwise,
* without the prior written permission of Suick Bay Technologies.
*
* Spread the word and not the disk.
*
* SPK 012290 : Initial
*/
#include "SystemPub.h"
#include "Proc.h"
#include "ShellPub.h"
#include "path.h"
#define BUFSIZE 256
/*******************************************************************
*
* Function CP
*
* PathName Callback function
*
* usage CP file file
* CP files directory
*
*******************************************************************/
#define cpVRefNum (**MyShell).Proc[ProcID].int0
#define cpDirID (**MyShell).Proc[ProcID].long0
#define cpAbort (**MyShell).Proc[ProcID].bflags.f0
Boolean CPSetDestDir( WHandle ShellWh, int16 ProcID, char *argument )
{
int16 err;
pathType pt;
char dirPath[ 256 ];
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
strcpy( dirPath, argument );
pt = SetCurrPath( dirPath );
if( pt == pathIsDir )
{
err = HGetVol( NULL, &cpVRefNum, &cpDirID );
if( err )
FileError( err );
}
else
err = 1;
ResetShellPWD( ShellWh );
return( err == noErr );
}
/*******************************************************************/
void CPCallBack( WHandle ShellWh, int16 ProcID, char *path, char *last,
pathType what, int16 vRefNum, int32 dirID )
{
char sName[ 64 ], dName[ 64 ];
OSErr err;
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
if( UserAbort() )
cpAbort = TRUE;
if( cpAbort )
return;
strcpy( sName, last );
strcpy( dName, last ); /* keep the same name in the new directory */
if( what == pathIsFile )
err = CopyFile( sName, dName, vRefNum, cpVRefNum, dirID, cpDirID );
else if( what == pathIsDir )
err = CopyDir( sName, dName, vRefNum, cpVRefNum, dirID, cpDirID );
if( err )
procPrintf( ShellWh, ProcID, "cp : can't copy %s (%d).\n", last, err );
}
/*******************************************************************/
void CPFiles( WHandle ShellWh, int16 ProcID, char *argument )
{
ShellWindRec **MyShell;
MyShell = (ShellWindRec **) (**ShellWh).thing;
ExpandPath( ShellWh, ProcID, argument, (ProcPtr) CPCallBack,
(**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
ResetShellPWD( ShellWh );
}
/*******************************************************************/
Boolean CPFrom1To2( WHandle ShellWh, int16 ProcID, char *file1, char *file2 )
{
int16 srcVref, dstVref, err;
int32 srcDirID, dstDirID, temp;
pathType pt;
char sName[ 64 ], dName[ 64 ], *file;
pt = SetCurrPath( file1 );
if( pt == pathIsFile )
{
strcpy( sName, GetLastScan() );
GetPWDInfo( &srcVref, &srcDirID, &temp );
ResetShellPWD( ShellWh );
pt = SetCurrPath( file2 );
if( pt != pathIsDir )
{
GetPWDInfo( &dstVref, &dstDirID, &temp );
if( pt == pathIsFile ) /* file exists, delete it */
{
strcpy( dName, GetLastScan() );
CtoPstr( dName );
err = HDelete( dstVref, dstDirID, dName );
if( err )
procPrintf( ShellWh, ProcID, "cp : can't delete %ps (%d).\n", dName, err );
PtoCstr( dName );
}
else /* does not exist, use given name */
{
file = ExtractFile( file2 );
strcpy( dName, file );
}
err = CopyFile( sName, dName, srcVref, dstVref, srcDirID, dstDirID );
if( err )
procPrintf( ShellWh, ProcID, "cp : can't copy %s (%d).\n", file1, err );
}
else
procPrintf( ShellWh, ProcID, "cp : can't copy directory %s.\n", sName );
}
ResetShellPWD( ShellWh );
}
/*******************************************************************/
Boolean DoCP( int16 ProcToken, WHandle ShellWh, int16 ProcID, char *string )
{
int16 i, argc;
char *cp, argument[ 256 ];
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
switch( ProcToken )
{
case PROC_INIT :
(**MyShell).Proc[ ProcID ].flags = TRUE;
break;
case PROC_TERM :
case PROC_BREAK :
cpAbort = TRUE;
/* Tell the shell that we're done */
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* Turn ourself off */
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
break;
case PROC_STDIN :
if( (**MyShell).Proc[ ProcID ].flags )
{
(**MyShell).Proc[ ProcID ].flags = FALSE;
argc = (**MyShell).Proc[ ProcID ].argc;
cpAbort = FALSE;
GetArgv( ShellWh, ProcID, argc-1, argument );
if( CPSetDestDir( ShellWh, ProcID, argument ) )
{
/*
* Copy each file to the destination directory
*/
for( i = 1; i < argc-1; i++ )
{
GetArgv( ShellWh, ProcID, i, argument );
CPFiles( ShellWh, ProcID, argument );
}
}
else if( argc == 3 )
{ /* last argument was a file and two arguments */
char dstfile[ 256 ];
GetArgv( ShellWh, ProcID, 1, argument );
GetArgv( ShellWh, ProcID, 2, dstfile );
CPFrom1To2( ShellWh, ProcID, argument, dstfile );
}
/* Tell the shell that we're done */
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* Turn ourself off */
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
return( FALSE );
}
}
}
/*******************************************************************
*
* Function MV
*
* PathName Callback function
*
* usage mv file file
* mv files directory
*
*******************************************************************/
#define mvVRefNum (**MyShell).Proc[ProcID].int0
#define mvDirID (**MyShell).Proc[ProcID].long0
#define mvAbort (**MyShell).Proc[ProcID].bflags.f0
Boolean MVSetDestDir( WHandle ShellWh, int16 ProcID, char *argument )
{
int16 err;
pathType pt;
char dirPath[ 256 ];
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
strcpy( dirPath, argument );
pt = SetCurrPath( dirPath );
if( pt == pathIsDir )
{
err = HGetVol( NULL, &mvVRefNum, &mvDirID );
if( err )
FileError( err );
}
else
err = 1;
ResetShellPWD( ShellWh );
return( err == noErr );
}
/*******************************************************************/
void MVCallBack( WHandle ShellWh, int16 ProcID, char *path, char *last,
pathType what, int16 vRefNum, int32 dirID )
{
char sName[ 64 ], dName[ 64 ];
OSErr err;
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
if( UserAbort() )
mvAbort = TRUE;
if( mvAbort )
return;
strcpy( sName, last );
strcpy( dName, last ); /* keep the same name in the new directory */
err = MoveFile( sName, dName, vRefNum, mvVRefNum, dirID, mvDirID );
if( err )
procPrintf( ShellWh, ProcID, "mv : can't move %s (%d).\n", last, err );
}
/*******************************************************************/
void MVFiles( WHandle ShellWh, int16 ProcID, char *argument )
{
ShellWindRec **MyShell;
MyShell = (ShellWindRec **) (**ShellWh).thing;
ScanExpFwd( FALSE ); /* scan directories backwards to do wildcard moves */
ExpandPath( ShellWh, ProcID, argument, (ProcPtr) MVCallBack,
(**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
ScanExpFwd( TRUE );
ResetShellPWD( ShellWh );
}
/*******************************************************************/
Boolean MVFrom1To2( WHandle ShellWh, int16 ProcID, char *file1, char *file2 )
{
int16 srcVref, dstVref, err;
int32 srcDirID, dstDirID, temp;
pathType pt;
char sName[ 64 ], dName[ 64 ], *file;
pt = SetCurrPath( file1 );
if( pt == pathIsFile )
{
strcpy( sName, GetLastScan() );
GetPWDInfo( &srcVref, &srcDirID, &temp );
ResetShellPWD( ShellWh );
pt = SetCurrPath( file2 );
if( pt != pathIsDir )
{
GetPWDInfo( &dstVref, &dstDirID, &temp );
if( pt == pathIsFile ) /* file exists, delete it */
{
strcpy( dName, GetLastScan() );
CtoPstr( dName );
err = HDelete( dstVref, dstDirID, dName );
if( err )
procPrintf( ShellWh, ProcID, "mv : can't delete %ps (%d).\n",
dName, err );
PtoCstr( dName );
}
else /* does not exist, use given name */
{
file = ExtractFile( file2 );
strcpy( dName, file );
}
err = MoveFile( sName, dName, srcVref, dstVref, srcDirID, dstDirID );
if( err )
procPrintf( ShellWh, ProcID, "mv : can't move %s (%d)\n", file1, err );
}
else
procPrintf( ShellWh, ProcID, "mv : can't move directory %s.\n", sName );
}
ResetShellPWD( ShellWh );
}
/*******************************************************************/
Boolean DoMV( int16 ProcToken, WHandle ShellWh, int16 ProcID, char *string )
{
int16 i, argc;
char *cp, argument[ 256 ];
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
switch( ProcToken )
{
case PROC_INIT :
(**MyShell).Proc[ ProcID ].flags = TRUE;
break;
case PROC_TERM :
case PROC_BREAK :
mvAbort = TRUE;
/* Tell the shell that we're done */
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* Turn ourself off */
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
break;
case PROC_STDIN :
if( (**MyShell).Proc[ ProcID ].flags )
{
(**MyShell).Proc[ ProcID ].flags = FALSE;
argc = (**MyShell).Proc[ ProcID ].argc;
mvAbort = FALSE;
GetArgv( ShellWh, ProcID, argc-1, argument );
if( MVSetDestDir( ShellWh, ProcID, argument ) )
{
for( i = 1; i < argc-1; i++ )
{
GetArgv( ShellWh, ProcID, i, argument );
MVFiles( ShellWh, ProcID, argument );
}
}
else if( argc == 3 )
{ /* last argument was a file and two arguments */
char dstfile[ 256 ];
GetArgv( ShellWh, ProcID, 1, argument );
GetArgv( ShellWh, ProcID, 2, dstfile );
MVFrom1To2( ShellWh, ProcID, argument, dstfile );
}
/* Tell the shell that we're done */
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* Turn ourself off */
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
return( FALSE );
}
}
}
/*******************************************************************
*
* Function RM
*
* PathName Callback function
*
* usage RM [options] [names]
*
*
*******************************************************************/
#define rmForce (**MyShell).Proc[ProcID].bflags.f0
#define rmAsk (**MyShell).Proc[ProcID].bflags.f1
#define rmRecurse (**MyShell).Proc[ProcID].bflags.f2
#define rmAbort (**MyShell).Proc[ProcID].bflags.f3
void RMCallBack( WHandle ShellWh, int16 ProcID, char *path, char *last,
pathType what, int16 vRefNum, int32 dirID )
{
OSErr err;
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
if( UserAbort() )
rmAbort = TRUE;
if( rmAbort )
return;
if( what == pathIsFile )
{
CtoPstr( last );
err = HDelete( vRefNum, dirID, last );
if( err )
FileError ( err );
}
else if( what == pathIsDir && rmRecurse )
{
err = RmTree( last, vRefNum, dirID );
if( err )
FileError ( err );
}
}
/*******************************************************************/
void RMFile( WHandle ShellWh, int16 ProcID, char *argument )
{
ShellWindRec **MyShell;
int16 matches = 1, lastMatch = 1;
MyShell = (ShellWindRec **) (**ShellWh).thing;
ScanExpFwd( FALSE ); /* scan directories backwards to do wildcard moves */
ExpandPath( ShellWh, ProcID, argument, (ProcPtr) RMCallBack,
(**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
ScanExpFwd( TRUE );
ResetShellPWD( ShellWh );
}
/*******************************************************************/
Boolean DoRM( int16 ProcToken, WHandle ShellWh, int16 ProcID, char *string )
{
int16 i, argc;
char *cp, argument[ 256 ];
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
switch( ProcToken )
{
case PROC_INIT :
(**MyShell).Proc[ ProcID ].flags = TRUE;
break;
case PROC_TERM :
case PROC_BREAK :
rmAbort = TRUE;
/* Tell the shell that we're done */
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* Turn ourself off */
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
break;
case PROC_STDIN :
if( (**MyShell).Proc[ ProcID ].flags )
{
(**MyShell).Proc[ ProcID ].flags = FALSE;
rmForce = FALSE;
rmAsk = FALSE;
rmRecurse = FALSE;
rmAbort = FALSE;
/* get arguments */
argc = (**MyShell).Proc[ ProcID ].argc;
for( i = 1; i < argc; i++ )
{
GetArgv( ShellWh, ProcID, i, argument );
cp = argument;
if( *cp++ == '-' )
while( *cp )
switch( *cp++ )
{
case 'f' : /* force */
rmForce = TRUE;
break;
case 'i' : /* ask for confirmation */
rmAsk = TRUE;
break;
case 'r' : /* recursively delete directories */
rmRecurse = TRUE;
break;
}
}
for( i = 1; i < (**MyShell).Proc[ ProcID ].argc; i++ )
{
GetArgv( ShellWh, ProcID, i, argument );
if( *argument != '-' )
RMFile( ShellWh, ProcID, argument );
}
/* Tell the shell that we're done */
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* Turn ourself off */
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
return( FALSE );
}
}
}